package com.sdy.healthcode.biz.dto.holiday;

import com.sdy.healthcode.biz.model.Holiday;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Data
@Accessors(chain = true)
@ApiModel(value = "节假日")
public class HolidayYearDTO {
    @ApiModelProperty(value = "年份(不传则为本年)")
    private Integer year;
    @ApiModelProperty(value = "每月节假日列表")
    private List<HolidayMonthDTO> months;
    
    public static HolidayYearDTO fromHolidayList(Integer year, List<Holiday> holidayList) {
        holidayList.sort(Comparator.comparing(Holiday::getDay));
        HolidayYearDTO result = new HolidayYearDTO();
        result.year = year;
        result.months = new ArrayList<>();
        AtomicInteger lastMonth = new AtomicInteger(-1);
        holidayList.forEach(holiday -> {
            Calendar c = Calendar.getInstance();
            c.setTime(holiday.getDay());
            int month = c.get(Calendar.MONTH) + 1;
            if (month != lastMonth.get()) {
                result.getMonths().add(new HolidayMonthDTO().setMonth(month).setDays(new ArrayList<>()));
            }
            result.getMonths().get(result.getMonths().size() - 1).getDays().add(c.get(Calendar.DATE));
            lastMonth.set(month);
        });
        return result;
    }
    
    @Data
    @Accessors(chain = true)
    @ApiModel(value = "节假日-月item")
    public static class HolidayMonthDTO {
        @ApiModelProperty(value = "月份")
        private Integer month;
        @ApiModelProperty(value = "日列表")
        private List<Integer> days;
//        @ApiModelProperty(value = "标记 1-增加 2-删除", allowableValues = "1,2")
//        private Integer flag;
    }
}
